import { url_video } from "@/comm/api";
import { get, patch, post, remove } from "@/comm/request";
import { defineStore } from "pinia";

interface Video {
    _id: string;
    name: string;
    tags: string[];
}

// interface IState {
//     cur: Video;
//     list: Video[];
//     totalPages: number;
//     page: number;
//     limit: number;
//     tag: string;
// }

const defaultStore = {
    cur: null as any,
    list: [] as any[],
    totalPages: 0,
    page: 1,
    limit: 10,
    tag: "",
    well: true,
    well_poster: "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png",
    well_word: "文本内容"
}

export const useVideoStore = defineStore({
    id: "video",
    state: () => defaultStore,
    actions: {
        async resetTag(id: string) {
            this.tag = id;
            this.totalPages = 0;
            this.page = 1;
            this.load();
        },
        async load() {
            const param: any = { page: this.page, limit: this.limit };
            if (this.tag) {
                param.tag = this.tag;
            }
            let res: any = await get(url_video, param);
            console.log("res", res)
            this.list = res.data.list;
            this.totalPages = res.data.pagination.totalPages;
        },
        async add(data: any) {
            let { _id, ...param } = data;
            console.log("param", param)
            let res: any = await post(url_video, param, true);
            console.log("res", res)
            // this.list = res.data;
        },
        async update(data: any) {
            let { _id, ...param } = data;
            console.log("param", param)
            let res: any = await patch(url_video + "/" + _id, param, true);
            console.log("res", res)
            // this.list = res.data;
        },
        async remove(id: string) {
            let res: any = await remove(url_video + "/" + id);
            console.log("res", res)
            // this.list = res.data;
        },
        async download(id: string, img: string) {
            let res: any = await post(url_video + "/img", { id, img }, true);
            console.log("res", res)
            return res;
            // this.list = res.data;
        },

    }
});
